package pachet.doi;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapViewLab extends MapActivity {
	
	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.mapview);
	    MapView mpView = (MapView) findViewById(R.id.mapview);
	    mpView.setBuiltInZoomControls(true);
	    
	    LocationManager mlocManager =
	    	(LocationManager)getSystemService(Context.LOCATION_SERVICE);
	    
	    LocationListener mlocListener = new MyLocationListener(mpView.getController(), mpView);

	    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
	    Toast.makeText( getApplicationContext(),
				"Se colecteaza date de la GPS",	
				Toast.LENGTH_SHORT).show();
	    
	}

	public class MyLocationListener implements LocationListener {

		private MapController mapController;
		private MapView mapview;
		private Drawable marker;

		public MyLocationListener(MapController controller, MapView mapview) {
			this.mapController = controller;
			this.mapview = mapview;
			marker = getResources().getDrawable(
		    	     android.R.drawable.ic_menu_myplaces);
		    marker.setBounds(0, 0, marker.getIntrinsicWidth(), 
		    		marker.getIntrinsicHeight());
		}

		public void onLocationChanged(Location loc) {
			mapview.getOverlays().clear();
			double lat = loc.getLatitude();
			double longit = loc.getLongitude();
			String text = "Locatia curenta: " +
				"\nLatitud = " + lat +
				"\nLongitud = " + longit;
			
//			label.setText(text);
			Toast.makeText( getApplicationContext(),
					text,	
					Toast.LENGTH_SHORT).show();
	
			GeoPoint currentPoint = new GeoPoint(
				   (int)(lat  * 1000000),
				   (int)(longit * 1000000));
			mapController.animateTo(currentPoint);
			
			//set marker
			Drawable marker=getResources().getDrawable(
				     android.R.drawable.star_on);
			marker.setBounds(0, 0, marker.getIntrinsicWidth(), 
				     marker.getIntrinsicHeight());
			Itemization items = new Itemization(marker);
			OverlayItem newPoint = new OverlayItem(currentPoint, "", "");
			
			items.addOverlay(newPoint);
			mapview.getOverlays().add(items);
			
			mapController.setZoom(16);
		}
	
		public void onProviderDisabled(String provider) {
		
			Toast.makeText( getApplicationContext(),
					"Gps Disabled",
					Toast.LENGTH_SHORT ).show();
		
		}

		public void onProviderEnabled(String provider) {
			Toast.makeText( getApplicationContext(),
					"Gps Enabled",
					Toast.LENGTH_SHORT).show();
		}

		public void onStatusChanged(String provider, int status, Bundle extras)	{
		}
		
	}
	
	public class Itemization extends ItemizedOverlay<OverlayItem> {

	    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

	    public Itemization(Drawable defaultMarker) {

	        super(boundCenterBottom(defaultMarker));
	        // super(defaultMarker);

	    }

	    @Override
	    protected OverlayItem createItem(int i) {
	        return mOverlays.get(i);
	    }

	    public void addOverlay(OverlayItem overlay) {
	        mOverlays.add(overlay);
	        populate();
	    }

	    @Override
	    public int size() {
	        return mOverlays.size();
	    }

	}

}
